/*******************************************************************************
 * @project: Java_Reflect
 * @package: com.burns.java.reflect
 * @file: InstantiationObjectByReflect.java
 * @author: Administrator
 * @created: 2017年5月13日
 * @purpose:
 * 
 * @version: 1.0
 * 
 * Revision History at the end of file.
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/

package com.burns.java.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 通过反射机制实例化一个类的对象
 * 
 * @author Administrator
 *
 */
public class InstantiationObjectByReflect {

	public static void main(String[] args) throws ClassNotFoundException, InstantiationException,
			IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Class<?> class1 = null;
		class1 = Class.forName("com.burns.java.reflect.User");
		// 第一种防范，实例化默认构造方法，调用set赋值
		User user = (User) class1.newInstance();

		user.setAge(20);
		user.setName("Rollen");
		System.out.println(user);

		// 第二种方法，取的全部的构造函数，使用构造函数赋值
		Constructor<?> cons[] = class1.getConstructors();
		// 查看每个构造方法需要的参数
		for (int i = 0; i < cons.length; i++) {
			Class<?> clazzs[] = cons[i].getParameterTypes();
			System.out.print("cons[" + i + "](");
			for (int j = 0; j < clazzs.length; j++) {
				if (j == clazzs.length - 1) {
					System.out.print(clazzs[j].getName());
				} else {
					System.out.print(clazzs[j].getName() + ",");
				}
			}
			System.out.println(")");
		}
		System.out.println("------------------------");
		//无参构造方法
		if (cons[0].getParameterTypes().length == 0) {
			user = (User) cons[0].newInstance();
			System.out.println(user);
		} else if (cons[0].getParameterTypes().length == 1) {
			user = (User) cons[0].newInstance("Rollen");
			System.out.println(user);
		} else if (cons[0].getParameterTypes().length == 2) {
			user = (User) cons[0].newInstance(20, "Rollen");
			System.out.println(user);
		}
		//只有一个构造方法
		if (cons[1].getParameterTypes().length == 0) {
			user = (User) cons[1].newInstance();
			System.out.println(user);
		} else if (cons[1].getParameterTypes().length == 1) {
			user = (User) cons[1].newInstance("Rollen");
			System.out.println(user);
		} else if (cons[1].getParameterTypes().length == 2) {
			user = (User) cons[1].newInstance(20, "Rollen");
			System.out.println(user);
		}
		//只有两个构造方法
		if (cons[2].getParameterTypes().length == 0) {
			user = (User) cons[2].newInstance();
			System.out.println(user);
		} else if (cons[2].getParameterTypes().length == 1) {
			user = (User) cons[2].newInstance("Rollen");
			System.out.println(user);
		} else if (cons[2].getParameterTypes().length == 2) {
			user = (User) cons[2].newInstance(20, "Rollen");
			System.out.println(user);
		}

	}
}

/**
 * 结果：
 * User [age=20, name=Rollen]
cons[0](int,java.lang.String)
cons[1](java.lang.String)
cons[2]()
------------------------
User [age=20, name=Rollen]
User [age=0, name=Rollen]
User [age=0, name=null]

 * @author Administrator
 *
 */

class User {

	private int age;
	private String name;

	public User() {
		super();
	}

	public User(String name) {
		super();
		this.name = name;
	}

	public User(int age, String name) {
		super();
		this.age = age;
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "User [age=" + age + ", name=" + name + "]";
	}
}

/*******************************************************************************
 * <B>Revision History</B><BR>
 * [type 'revision' and press Alt + / to insert revision block]<BR>
 * 
 * 
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/